var flag = true; //标记位

function FocusItem(obj) {
    if ($(obj).attr('name') == 'verycode') {
        $(obj).next().next().html('').remove('error');
    }else{
        $(obj).next('span').html('').remove('error');
    }
}
function CheckItem(obj) {
    var msgBox =$(obj).next('span');
    switch ($(obj).attr('name')){
        case "pass":
            if (obj.value == ""){
                msgBox.html('请输入原密码');
                msgBox.addClass('error');
                flag = false;
            }else {flag = true;}
            break;
        case "newpass":
            var pwd = /^[0-9a-zA-Z]{6,10}$/;
            if (obj.value == ""){
                msgBox.html('请输入新密码');
                msgBox.addClass('error');
                flag = false;
            }else if(!pwd.test(obj.value)){
                msgBox.html('新密码格式不正确');
                msgBox.addClass('error');
                flag = false;
            }else{ flag = true;}
            break;
        case "againpass":
            if (obj.value == ""){
                msgBox.html('请再次输入新密码');
                msgBox.addClass('error');
                flag = false;
            }else  if ($(obj).val() != $('input[name="newpass"]').val()) {
                msgBox.html('两次密码输入不一致');
                msgBox.addClass('error');
                flag = false;
            }else { flag = true;}
            break;
        case "verycode":
            var numshow = $(obj).next().next();
            if (obj.value == ""){
                numshow.html('验证码不能为空');
                numshow.addClass('error');
                flag = false;
            }else{
                var url = "http://localhost:8080/lyq/admin/checkusernum?num="+encodeURI($(obj).val())+"&"+new Date().getTime();
                $.get(url, function (nume) {
                    if (nume == 'false'){
                        numshow.html('验证码不正确');
                        numshow.addClass('error');
                        flag = false;
                    } else{
                        numshow.html().removeClass('error');
                        flag = true;
                    }
                })
            }
            break;
    }
}

function checkForm(frm) {
    var els = frm.getElementsByTagName('input');
    //有onblur属性的才需要验证
    for (var i = 0;i<els.length;i++){
        if (els[i]!=null){
            if (els[i].getAttribute("onblur")){
                CheckItem(els[i]);
            }
        }
    }
    return flag;
}